//
//  AESubmitOrderTopView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/14.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AESubmitOrderTopView.h"
#import "AE_Solar-Swift.h"

@interface AESubmitOrderTopView ()


@property (nonatomic, strong) UIView *navView;

/// 返回
@property (nonatomic, strong) UIButton *btnBack;

@property (nonatomic, strong) UILabel *navTitleLabel;


@property (nonatomic, strong) UIImageView *infoBgImgView;

/// 标题
@property (nonatomic, strong) UILabel *titleLabel;

/// 缩略图
@property (nonatomic, strong) UIImageView *thumalImgView;

/// 积分
@property (nonatomic, strong) UILabel *cntLabel;

/// 商品名称
@property (nonatomic, strong) UILabel *goodsNameLabel;

/// 剩余数量
@property (nonatomic, strong) UILabel *remainingNumLabel;


/// 标题
- (void)initWithTitleLabel;

/// 缩略图
- (void)initWithThumalImgView;

/// 积分
- (void)initWithCntLabel;

/// 商品名称
- (void)initWithGoodsNameLabel;

/// 剩余数量
- (void)initWithRemainingNumLabel;

@end

@implementation AESubmitOrderTopView

- (void)initWithSubView {
//    self.backgroundColor = [UIColor colorWithHexString:@"#F20911"];
    
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = self.bounds;
    gl.startPoint = CGPointMake(0.36, 0.22);
    gl.endPoint = CGPointMake(0.42, 0.96);
    UIColor *startColor = UIColorFromRGBA(0xF20911, 1);
    UIColor *endColor = UIColorFromRGBA(0xFF7B7F, 0);
    gl.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [self.layer addSublayer:gl];
    
    
    [self initWithNavView];
    
    [self initWithbtnBack];
    
    [self initWithNavTitleLabel];
}

- (void)initWithNavView {
    if (!_navView) {
        CGRect frame = CGRectMake(0, SYS_STATUSBAR_HEIGHT + IPHONEX_TOP_SPACE, self.width, NAVBAR_HEIGHT_NOTOPSPACE);
        _navView = [[UIView alloc] initWithFrame:frame];
//        _navView.backgroundColor = [UIColor orangeColor];
        [self addSubview:_navView];
    }
}

/// 返回
- (void)initWithbtnBack {
    if (!_btnBack) {
        UIImage *image = [UIImage imageNamed:@"ic_nav_back_white"];
        CGRect frame = CGRectMake(4, (self.navView.height - 30) * 0.5, 30, 30);
        _btnBack = [UIButton createWithButton:self.navView frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithBack:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
}

- (void)initWithNavTitleLabel {
    if (!_navTitleLabel) {
        NSString *title = [AEStringExten localizedValueWithKey:@"提交订单"];
        UIFont *font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        CGRect frame = CGRectMake(80, (self.navView.height - 30) * 0.5, self.navView.width - 80 * 2, 30);
        _navTitleLabel = [UILabel createWithLabel:self.navView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = kWhiteColor;
            label.text = title;
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
}

- (void)initWithInfoBgImgView {
    if (!_infoBgImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_order_goods_info"];
        CGRect frame = CGRectMake(10, self.height - image.size.height - 10, self.width - 20, image.size.height);
        _infoBgImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

/// 标题
- (void)initWithTitleLabel {
    if (!_titleLabel) {
        UIFont *font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
        NSString *title = [AEStringExten localizedValueWithKey:@"商品"];
        CGRect frame = CGRectMake(14, 10, self.infoBgImgView.width - 14 * 2, 22);
        _titleLabel = [UILabel createWithLabel:self.infoBgImgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = title;
        }];
    }
}

/// 缩略图
- (void)initWithThumalImgView {
    if (!_thumalImgView) {
        CGRect frame = CGRectMake(14, self.titleLabel.bottom + 6, 79, 79);
        _thumalImgView = [UIImageView createWithImageView:self.infoBgImgView frame:frame defaultImage:nil propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
            imageView.layer.cornerRadius = 4;
            imageView.layer.masksToBounds = YES;
        }];
    }
}

/// 积分
- (void)initWithCntLabel {
    if (!_cntLabel) {
        CGRect frame = CGRectMake(0, self.thumalImgView.top, 0, 20);
        _cntLabel = [UILabel createWithLabel:self.infoBgImgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#F6020A"];
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    
    NSString *cnt = [AEStringExten localizedValueWithKey:@"积分"];
    NSString *title = [NSString stringWithFormat:@"889%@", cnt];
    float width = [title widthForFont:self.cntLabel.font] + 2;
    self.cntLabel.width = width;
    self.cntLabel.text = title;
    self.cntLabel.left = self.infoBgImgView.width - 14 - width;
}

/// 商品名称
- (void)initWithGoodsNameLabel {
    if (!_goodsNameLabel) {
        CGRect frame = CGRectMake(self.thumalImgView.right + 7, self.thumalImgView.top, self.cntLabel.left - self.thumalImgView.right - 7 - 10, 22);
        _goodsNameLabel = [UILabel createWithLabel:self.infoBgImgView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    self.goodsNameLabel.text = @"户外背包";
}

/// 剩余数量
- (void)initWithRemainingNumLabel {
    if (!_remainingNumLabel) {
        CGRect frame = CGRectMake(self.goodsNameLabel.left, self.goodsNameLabel.bottom + 5, 0, 17);
        UIFont *font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
        _remainingNumLabel = [UILabel createWithLabel:self.infoBgImgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#303939"];
            label.backgroundColor = [UIColor colorWithHexString:@"#F4F7F8"];
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    NSString *tmpStr = [AEStringExten localizedValueWithKey:@"剩余"];
    NSString *title = [NSString stringWithFormat:@"%@23", tmpStr];
    float width = [title widthForFont:self.remainingNumLabel.font] + 16;
    _remainingNumLabel.text = title;
    _remainingNumLabel.width = width;
}

/// 返回
/// - Parameter sender: sender description
- (void)actionWithBack:(id)sender {
    if (self.touchBackBlock) {
        self.touchBackBlock();
    }
}

- (void)updateWithViewData:(id)data {
    
    [self initWithInfoBgImgView];
    
    /// 标题
    [self initWithTitleLabel];

    /// 缩略图
    [self initWithThumalImgView];

    /// 积分
    [self initWithCntLabel];

    /// 商品名称
    [self initWithGoodsNameLabel];

    /// 剩余数量
    [self initWithRemainingNumLabel];
}

@end
